package com.zhuhjay.eduorder.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zhuhjay.commonutils.ordervo.CourseOrder;
import com.zhuhjay.commonutils.ordervo.MemberOrder;
import com.zhuhjay.eduorder.client.CourseInfoClient;
import com.zhuhjay.eduorder.client.MemberInfoClient;
import com.zhuhjay.eduorder.entity.Order;
import com.zhuhjay.eduorder.mapper.OrderMapper;
import com.zhuhjay.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuhjay.eduorder.utils.OrderNoUtil;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private CourseInfoClient courseInfoClient;

    @Resource
    private MemberInfoClient memberInfoClient;

    @Override
    public String createOrder(String courseId, String memberId) {
        // 远程访问课程信息
        CourseOrder courseInfoOrder = courseInfoClient.getCourseInfoOrder(courseId);
        // 远程访问会员信息
        MemberOrder memberInfoOrder = memberInfoClient.getMemberInfoOrder(memberId);

        // 订单信息合成
        Order order = new Order()
                // 课程信息
                .setCourseCover(courseInfoOrder.getCover())
                .setCourseId(courseInfoOrder.getId())
                .setCourseTitle(courseInfoOrder.getTitle())
                .setTeacherName(courseInfoOrder.getTeacherName())
                // 用户信息
                .setMemberId(memberInfoOrder.getId())
                .setMobile(memberInfoOrder.getMobile())
                .setNickname(memberInfoOrder.getNickname())
                // 订单其余信息
                .setPayType(1)
                .setStatus(0)
                .setOrderNo(OrderNoUtil.getOrderNo());
        // 保存订单数据
        this.save(order);

        return order.getOrderNo();
    }
}
